package cn.kgc.vue.controller;

import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.entity.Permission;
import cn.kgc.vue.entity.RolePers;
import cn.kgc.vue.service.PermissionService;
import cn.kgc.vue.service.RolePersService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/***
 *@title MenuController
 *@author 韩旭
 *@version 1.0.0
 *@create 2023/8/15 14:43
 **/
@RestController
@RequestMapping("permission")
public class MenuController {
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RolePersService rolePersService;

    @GetMapping("list")
    public ResponseResult list() {
        List<Permission> list = permissionService.list();
        ResponseResult responseResult = new ResponseResult();
        responseResult.success(ResponseStatus.SUCCESS, list);
        return responseResult;
    }

    @PostMapping("addorupdate")
    public ResponseResult addOrupdate(@RequestBody Permission permission) {
        permissionService.saveOrUpdate(permission);
        ResponseResult responseResult = new ResponseResult();
        responseResult.success();
        return responseResult;
    }

    @GetMapping("getone/{id}")
    public ResponseResult getone(@PathVariable("id") String id) {
        ResponseResult responseResult = new ResponseResult();
        HashMap<String, Object> map = new HashMap<>();
        Permission permission = permissionService.getById(id);
        Integer pid = permission.getPid();
        if (pid == 0) {
            map.put("permission", permission);
            responseResult.success(ResponseStatus.SUCCESS, map);
            return responseResult;
        }
        Permission permission1 = permissionService.getById(pid);
        String title = permission1.getTitle();
        map.put("permission", permission);
        map.put("parentTitle", title);
        responseResult.success(ResponseStatus.SUCCESS, map);
        return responseResult;
    }

    @DeleteMapping("delete/{id}")
    @Transactional
    public ResponseResult delete(@PathVariable("id") String id) {
        ResponseResult responseResult = new ResponseResult();
        LambdaQueryWrapper<Permission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Permission::getPid, id);
        List<Permission> list = permissionService.list(wrapper);
        if (ObjectUtils.isNotEmpty(list)) {
            responseResult.fail(ResponseStatus.HAS_CHILDNODE);
            return responseResult;
        }
        LambdaQueryWrapper<RolePers> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(RolePers::getPerId, id);
        rolePersService.remove(wrapper1);
        permissionService.removeById(id);
        responseResult.success(ResponseStatus.DELETE_SUCCESS);
        return responseResult;
    }

    @GetMapping("search")
    public ResponseResult search(@RequestParam("titleName") String titleName) {
        ResponseResult responseResult = new ResponseResult();
        LambdaQueryWrapper<Permission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Permission::getTitle,titleName);
        Permission permission = permissionService.getOne(wrapper);
        responseResult.success(ResponseStatus.SUCCESS,permission);
        return responseResult;
    }


}
